# +----------------------------------------------------------------------
# | ChatWork智能聊天办公系统
# +----------------------------------------------------------------------
# | 软件声明: 本系统并非自由软件,未经授权任何形式的商业使用均属非法。
# | 版权保护: 任何企业和个人不允许对程序代码以任何形式任何目的复制/分发。
# | 授权要求: 如有商业使用需求,请务必先与版权所有者取得联系并获得正式授权。
# +----------------------------------------------------------------------
# | Author: ChatWork Team <2474369941@qq.com>
# +----------------------------------------------------------------------
from typing import List
from fastapi import APIRouter, Request, Depends, UploadFile, Form
from fastapi.responses import StreamingResponse
from hypertext import R, response_json
from common.chain.chain_server import ChatUtils
from apps.api.schemas.apply.docs import files_schema as schema
from apps.api.service.apply.docs.file_service import DocsFilesService
from apps.api.widgets.chat_docs_widget import ChatDocsWidget

router = APIRouter(prefix="/files", tags=["畅阅文件接口"])


@router.get("/prompts", summary="模式列表", response_model=R[List[schema.FilesPromptsVo]])
@response_json
async def prompts():
    return await DocsFilesService.prompts()


@router.get("/lists", summary="文件列表", response_model=R[List[schema.FilesListVo]])
@response_json
async def lists(request: Request, params: schema.FilesSearchIn = Depends()):
    user_id: int = request.state.user_id
    return await DocsFilesService.lists(params, user_id)


@router.get("/detail", summary="文件详情", response_model=R[schema.FilesDetailVo])
@response_json
async def detail(request: Request, params: schema.FilesDetailIn = Depends()):
    user_id: int = request.state.user_id
    return await DocsFilesService.detail(params.code, user_id)


@router.post("/rename", summary="文件命名", response_model=R)
@response_json
async def rename(request: Request, params: schema.FilesRenameIn):
    user_id: int = request.state.user_id
    return await DocsFilesService.rename(params, user_id)


@router.post("/delete", summary="文件删除", response_model=R)
@response_json
async def delete(request: Request, params: schema.FilesDeleteIn):
    user_id: int = request.state.user_id
    return await DocsFilesService.delete(params, user_id)


@router.post("/upload", summary="文件上传", response_model=R[schema.FilesUploadVo])
@response_json
async def upload(request: Request, file: UploadFile, mode: str = Form()):
    user_id: int = request.state.user_id
    ip: str = request.client.host
    return await DocsFilesService.upload(file, mode, user_id, ip)


@router.post("/chat", summary="文件提问")
async def chat(request: Request, params: schema.FilesDialogSendIn):
    try:
        user_id: int = request.state.user_id
        user_ip: str = request.client.host

        # 对话参数
        chat_params = {
            "type": params.type,
            "code": params.code,
            "question": params.question,
            "user_id": user_id,
            "ip": user_ip
        }

        generate = ChatDocsWidget(request, chat_params).chat()
        return StreamingResponse(generate, media_type="text/event-stream")
    except Exception as e:
        generate = ChatUtils.output_error(message=str(e))
        return StreamingResponse(generate, media_type="text/event-stream")
